<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="分类名称" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="openDialog"
          >新建分类</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData">
      <!-- <el-table-column label="分类ID" prop="id" width="120" /> -->
      <el-table-column
        label="分类名称"
        prop="categoryName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="商品数" prop="goodsNum" />
      <el-table-column label="更新人" prop="updateByName">
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.updateByName) }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序值" prop="sort" />
      <el-table-column
        label="操作"
        align="center"
      >
        <template slot-scope="scope">
          <div class="operate-box">
            <el-button type="text" @click="openDialog('edit', scope.row)"
              >编辑</el-button
            >
            |
            <el-button type="text" @click="handleRemove(scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="dialogTitle"
      :visible.sync="addOrUpdateDialog"
      width="600px"
      append-to-body
      custom-class="custom-dialog"
      @close="closeDialog" :close-on-click-modal="false"
    >
      <el-form
        ref="categoryFormRef"
        :model="categoryForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="分类名称：" prop="categoryName">
          <el-input
            v-model="categoryForm.categoryName"
            placeholder="请输入，限10个字"
            maxlength="10"
          />
        </el-form-item>
        <el-form-item label="排序值：" prop="sort">
          <el-input
            v-number-input
            maxlength="5"
            v-model="categoryForm.sort"
            placeholder="限输入数字"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('categoryFormRef')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategoryListAPI,
  addCategoryAPI,
  deleteCategoryAPI,
  updateCategory,
} from "@/api/system/product";

export default {
  name: "Catgory",
  data() {
    return {
      loading: false,
      ids: [],
      dateRange: [],
      tableData: [],
      total: 1,
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        categoryName: "",
      },
      addOrUpdateDialog: false,
      dialogTitle: "",
      categoryForm: {
        categoryName: "",
        sort: "",
      },
      rules: {
        categoryName: [
          {
            required: true,
            message: "分类名称必填，限输入10个字",
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: "排序值必填，限输入5个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.loading = true;
      try {
        const res = await getCategoryListAPI(this.queryParams);
        this.tableData = res.rows;
        this.total = res.total;
      } finally {
        this.loading = false;
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleRemove(row) {
      this.$confirm("删除后前台不可见, 是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteCategoryAPI(row.id).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async openDialog(type, row) {
      this.dialogTitle = type === "edit" ? "编辑分类" : "新建分类";
      this.addOrUpdateDialog = true;
      if (type === "edit") {
        this.categoryForm = { ...row };
      }
    },
    closeDialog() {
      this.addOrUpdateDialog = false;
      this.resetForm("categoryFormRef");
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid, errorObj) => {
        if (valid) {
          if (this.categoryForm.id != undefined) {
            updateCategory(this.categoryForm).then((res) => {
              if (res.code === 200) {
                this.$message({
                  type: "success",
                  message: "编辑成功!",
                });
                this.getList();
                this.closeDialog();
              }
            });
          } else {
            addCategoryAPI(this.categoryForm).then((res) => {
              if (res.code === 200) {
                this.$message({
                  type: "success",
                  message: "新增成功!",
                });
                this.getList();
                this.closeDialog();
              }
            });
          }
        }
      });
    },
  },
};
</script>
